﻿namespace Pp.Repository
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Core;
    using Domain.Model;
    using Globalization;
    using NHibernate;
    using NHibernate.Criterion;

    public class AngelRequestRepository : BaseRepository<AngelRequest>
    {
        public AngelRequestRepository(ISession session, HttpServerUtilityBase server)
            : base(session, server)
        {
        }
        // Proje sahibi sponsorluk başvuruları
        public IList<AngelRequest> GetAllNewByProject(Guid? projectId)
        {
            return BaseCriteria
                .CreateAlias("Project", "p")
                .Add(Restrictions.Eq("p.Id", projectId))
                .Add(Restrictions.Eq("State", AngelRequestState.New)).List<AngelRequest>();
        }
        // Proje sahibi Onayladığı Sponsorlar
        public IList<AngelRequest> GetAllApproByProject(Guid? projectId)
        {
            return BaseCriteria
                .CreateAlias("Project", "p")
                .Add(Restrictions.Eq("p.Id", projectId))
                .Add(Restrictions.Eq("State", AngelRequestState.Appro)).List<AngelRequest>();
        }
        //Sponsor Tarafı : Sponsoru olduğu projeler
        public IList<AngelRequest> GetAllAngelRequestByProject(Guid? userId)
        {
            return BaseCriteria
                .CreateAlias("User", "u")
                .Add(Restrictions.Eq("u.Id", userId))
                .Add(Restrictions.Eq("State", AngelRequestState.New)).List<AngelRequest>();
        }
        //Sponsor Tarafı : Sponsoru olduğu projeler
        public IList<AngelRequest> GetAllAngelRequestCanByProject(Guid? userId)
        {
            return BaseCriteria
                .CreateAlias("User", "u")
                .Add(Restrictions.Eq("u.Id", userId))
                .Add(Restrictions.Eq("State", AngelRequestState.New)).List<AngelRequest>();
        }

        public Result Create(Project project, User user)
        {
            if (!project.AllowAngelRequest) return Result.AsError(Res.Information_Project_NoAccept_Angel);

            if (project.AngelRequests.Where(d => d.State == AngelRequestState.New && d.User == user).Count() > 0)
                return Result.AsError(Res.Information_There_is_a_request_angel_for_project);
            if (project.AngelRequests.Where(d => d.State == AngelRequestState.Appro && d.User == user).Count() > 0)
                return Result.AsError(Res.Information_You_are_Angel_for_this_project);
            if (project.AngelRequests.Where(d => d.State == AngelRequestState.Cancel && d.IsAgainRequest && d.User == user).Any())
            {
                return Result.AsError(Res.Information_Angel_Blocked);
            }
            var existRequest = project.AngelRequests.Where(d => d.User == user).FirstOrDefault();
            if (existRequest != null)
            {
                existRequest.State = AngelRequestState.New;
                Update(existRequest);
            }
            else
            {
                var angelRequest = new AngelRequest
                {
                    Project = project,
                    User = user,
                    State = AngelRequestState.New
                };
                Save(angelRequest);
            }
            return Result.AsSuccess();
        }

        public Result Cancel(Guid? angelRequestId)
        {
            var angelRequest = Get(angelRequestId);
            angelRequest.State = AngelRequestState.Cancel;
            Update(angelRequest);
            return Result.AsSuccess();
        }
        public Result Appro(Guid? angelRequestId)
        {
            var angelRequest = Get(angelRequestId);
            angelRequest.State = AngelRequestState.Appro;
            Update(angelRequest);
            return Result.AsSuccess();
        }
    }
}